<template>
  <div class="page-Body">
    <h2 class="page-title">资源消息群</h2>
    <!-- 表单 -->
    <el-form :inline="true" :model="formInline">
      <el-form-item label="状态">
        <el-input
          v-model="formInline.templateName"
          placeholder="请输入状态名称"
          clearable
          style="width: 250px"
          v-noSpace
        />
      </el-form-item>
      <el-form-item label="发起人">
        <el-input
          v-model="formInline.templateName"
          placeholder="请输入发起人"
          clearable
          style="width: 250px"
          v-noSpace
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <TimeFormatter
          @TimeChild="TimeHandle"
          ref="childComponentRef"
        ></TimeFormatter>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Search">搜索</el-button>
        <el-button @click="Reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 新增对话框组件 -->
    <el-button plain @click="AddOpenDialog" class="el-DiaButton" type="primary">
      添加
    </el-button>
    <Addlibrary
      @confirm="AddConfirm"
      @cancel="AddClose"
      :dialogAdd="dialogAdd"
    />
    <!-- 详情对话框 -->
    <Detailslibrary
      :DetailsRow="DetailsRow"
      :Detail="Detail"
      @cancel="DetClose"
    />
    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      stripe
      fit
      empty-text="暂无数据"
      show-header
      v-loading="loading"
      size="default"
      element-loading-text="Loading..."
      :header-cell-style="{ background: '#e7e1fb' }"
      style="width: 100%"
      max-height="550px"
    >
      <el-table-column type="index" width="80" label="序号" />
      <el-table-column prop="templateName" label="内容" />
      <el-table-column prop="typeName" label="发起时间" />
      <el-table-column prop="createTime" label="创建人" />
      <el-table-column prop="createByName" label="状态" />
      <el-table-column prop="createByName" label="应答数/帮到我" />
      <el-table-column align="center" fixed="right" label="操作" v-slot="scope">
        <el-button link type="primary" @click="Details(scope.row)">
          详情
        </el-button>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="pagination-block">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[20, 40, 80, 100]"
        background
        layout="total,sizes, prev, pager, next,jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { getActivityLibraryList } from '@/api/Activity/library.js';
import { ref, onMounted } from 'vue';
import Addlibrary from '@/components/Activity/library/Addlibrary.vue';
import Detailslibrary from '@/components/Activity/library/Detailslibrary.vue';
// 分页变量
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
// 对话框
const dialogAdd = ref(false);
const DetailsRow = ref({});
const Detail = ref(false);
// 表单-时间选择器
const childComponentRef = ref(null);
const TimeArray = ref([]);
// 表单数据
const formInline = ref({
  templateName: '',
  typeId: '',
});
// 列表数据+表单校验
const tableData = ref([]);
const loading = ref(true);
// 分页逻辑
const handleSizeChange = async (val) => {
  pageSize.value = val;
  ActivityLibraryList();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  ActivityLibraryList();
};
// 获取列表
const ActivityLibraryList = async () => {
  loading.value = true;
  const params = {
    current: currentPage.value,
    size: pageSize.value,
    query: { ...formInline.value },
  };

  // 只有当时间数组有值时才添加时间参数
  if (TimeArray.value && TimeArray.value.length > 0) {
    params.startDate = TimeArray.value[0];
    params.endDate = TimeArray.value[1];
  }
  const res = await getActivityLibraryList(params);

  if (res.data.code === 200) {
    tableData.value = [];
    total.value = res.data.data.counts || 0;
    loading.value = false;
  }
};
// 搜索校验
const hasSearchConditions = () => {
  const { templateName, typeId } = formInline.value;
  return (
    templateName || typeId || (TimeArray.value && TimeArray.value.length > 0)
  );
};
// 搜索
const Search = async () => {
  if (!hasSearchConditions()) {
    // eslint-disable-next-line no-undef
    ElMessage({
      message: '请至少输入一个搜索条件',
      type: 'warning',
    });
    return;
  }
  ActivityLibraryList();
};
// 重置
const Reset = () => {
  // 如果当前已经是空值状态，直接返回
  if (
    formInline.value.templateName === '' &&
    formInline.value.typeId === '' &&
    TimeArray.value.length === 0
  ) {
    return;
  }
  formInline.value = {};
  ActivityLibraryList();
};
//获取时间
const TimeHandle = (data) => {
  TimeArray.value = data;
};
// 详情
const Details = async (row) => {
  Detail.value = true;
  DetailsRow.value = row;
};
const DetClose = () => {
  Detail.value = false;
};
// 添加功能对话框
const AddOpenDialog = () => {
  dialogAdd.value = true;
};

const AddConfirm = () => {
  dialogAdd.value = false;
  ActivityLibraryList();
};
const AddClose = () => {
  dialogAdd.value = false;
};
onMounted(() => {
  ActivityLibraryList();
});
</script>

<style lang="scss" scoped>
.el-DiaButton {
  width: 120px;
  height: 40px;
  border-radius: 4px;
  margin-bottom: 20px;
  border: 1px solid #c7d4ff;
}
</style>
